<?php declare(strict_types=1);

namespace shali\ydb\think2fa;

use app\admin\middleware\AdminTokenMiddleware;
use shali\ydb\think2fa\controller\Admin;
use think\facade\App;
use think\Route;

class Service extends \think\Service
{
    /**
     * @var App
     */
    protected $app;

    protected $routeMiddleware = [];

    /**
     * 注册服务
     * @return void
     */
    public function register()
    {
        // 注入服务
        $this->app->bind('2fa', Think2Fa::class);
        // 注册路由中间件
        $this->registerRouteMiddleware();
    }

    /**
     * 执行服务
     * @param Route $route
     * @return void
     */
    public function boot(Route $route)
    {
        $route->get('/plugin/2fa/admin/index', Admin::class . '@index');
        $route->group('/plugin/2fa', function (Route $route) {
            $route->get('/admin/status', Admin::class . '@status');
            $route->put('/admin/updateStatus', Admin::class . '@updateStatus');
            $route->get('/admin/qrcode', Admin::class . '@qrcode');
            $route->post('/admin/verify', Admin::class . '@verify');
            $route->put('/admin/resetSecret', Admin::class . '@resetSecret');
        })->middleware($this->routeMiddleware);
    }

    protected function registerRouteMiddleware()
    {
        $this->routeMiddleware = [
            AdminTokenMiddleware::class
        ];
    }
}